﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.UI;
using Waiter.Common;
using Waiter.ServiceReference1;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

using Windows.UI.Popups;
using System.Diagnostics;
using Waiter.UserControls;



namespace Waiter
{
    
    public sealed partial class GiaoDienChonMon : Page
    {
        private NavigationHelper navigationHelper;
        private ObservableDictionary defaultViewModel = new ObservableDictionary();
        
        // Khi vào được giao diện này, nghĩa là nhân viên bồi bàn đã
        // click chọn bàn trước đó. Table này chính là bàn mà bồi đang phục vụ.
        public static Table table = new Table(); // Initialize later.
        

        public ObservableDictionary DefaultViewModel
        {
            get { return this.defaultViewModel; }
        }

        
        public NavigationHelper NavigationHelper
        {
            get { return this.navigationHelper; }
        }

        public GiaoDienChonMon()
        {
            this.InitializeComponent();
            this.navigationHelper = new NavigationHelper(this);
            this.navigationHelper.LoadState += navigationHelper_LoadState;

            // Test
            
        }


        private async void init()
        {
            pageTitle.Text = table.TableName;
            Service1Client client = new Service1Client();
            try
            {
                
                contentRegion.Children.Clear();
                GoiMonControl goiMonControl = new GoiMonControl(table);
                contentRegion.Children.Add(goiMonControl);


            }
            catch (Exception)
            {
                 MessageDialog dl = new MessageDialog("Không kết nối được với Service");
                dl.ShowAsync();
            }
        }
       
        
        
        private void navigationHelper_LoadState(object sender, LoadStateEventArgs e)
        {
            object navigationParameter;
            if (e.PageState != null && e.PageState.ContainsKey("SelectedItem"))
            {
                navigationParameter = e.PageState["SelectedItem"];
                table = navigationParameter as Table;
            }
            
        }

        #region NavigationHelper registration

        /// The methods provided in this section are simply used to allow
        /// NavigationHelper to respond to the page's navigation methods.
        /// 
        /// Page specific logic should be placed in event handlers for the  
        /// <see cref="GridCS.Common.NavigationHelper.LoadState"/>
        /// and <see cref="GridCS.Common.NavigationHelper.SaveState"/>.
        /// The navigation parameter is available in the LoadState method 
        /// in addition to page state preserved during an earlier session.

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            navigationHelper.OnNavigatedTo(e);
            table = e.Parameter as Table;
            init();
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            navigationHelper.OnNavigatedFrom(e);
            
        }

        #endregion

        private void ChonMonAppBarButton_Tapped(object sender, TappedRoutedEventArgs e)
        {
            Service1Client client = new Service1Client();
            try
            {

                contentRegion.Children.Clear();
                GoiMonControl goiMonControl = new GoiMonControl(table);
                contentRegion.Children.Add(goiMonControl);
                
            }
            catch (Exception)
            {
                MessageDialog dl = new MessageDialog("Không kết nối được với Service");
                dl.ShowAsync();
            }
            this.TopAppBar.IsOpen = false;
        }

        private void HoaDonAppBarButton_Tapped(object sender, TappedRoutedEventArgs e)
        {
            HoaDonViewer hoadonViewer = new HoaDonViewer(table);
            contentRegion.Children.Clear();
            contentRegion.Children.Add(hoadonViewer);
            this.TopAppBar.IsOpen = false;
        }

      
        

    }


    public static class WaiterDialog
    {
        public static Popup CreateDialog(UserControl content)
        {
            var _popup = new Popup();
            content.VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Center;
            var gr = new Grid
            {
                Width = Window.Current.Bounds.Width,
                Height = Window.Current.Bounds.Height,
                Background = new SolidColorBrush(Color.FromArgb(128, 0, 0, 0))
            };
            gr.Children.Add(content);
            _popup.Child = gr;
            return _popup;
        }
    }
}
